package com.acwing.partition2;

import java.io.*;
import java.util.LinkedList;

/**
 * @author `RKC`
 * @date 2021/12/28 14:56
 */
public class AC173矩阵距离 {

    private static final int N = 1010, M = 1010;
    private static final int[][] dirs = {{1, 0}, {0, 1}, {-1, 0}, {0, -1}};
    private static final int[][] matrix = new int[N][M], dist = new int[N][M];
    private static int n = 0, m = 0;

    private static final BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
    private static final BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(System.out));

    public static void main(String[] args) throws IOException {
        String[] s = reader.readLine().split("\\s+");
        n = Integer.parseInt(s[0]);
        m = Integer.parseInt(s[1]);
        for (int i = 0; i < n; i++) {
            String str = reader.readLine();
            for (int j = 0; j < m; j++) matrix[i][j] = str.charAt(j) - '0';
        }
        resolve();
        writer.flush();
    }

    private static void resolve() throws IOException {
        for (int i = 0; i < n; i++) {
            for (int j = 0; j < m; j++) dist[i][j] = -1;
        }
        //找所有0到最近的1的距离，相当于从每个1开始去更新最近的0；给每个1都连接一个虚拟节点，虚拟节点到0的距离，相当于所有的1到0的距离+1
        LinkedList<int[]> queue = new LinkedList<>();
        for (int i = 0; i < n; i++) {
            for (int j = 0; j < m; j++) {
                if (matrix[i][j] == 1) {
                    dist[i][j] = 0;
                    queue.addLast(new int[]{i, j});
                }
            }
        }
        while (!queue.isEmpty()) {
            int[] curr = queue.pollFirst();
            int x = curr[0], y = curr[1];
            for (int[] dir : dirs) {
                int i = dir[0] + x, j = dir[1] + y;
                if (i < 0 || i >= n || j < 0 || j >= m || dist[i][j] != -1) continue;
                dist[i][j] = dist[x][y] + 1;
                queue.addLast(new int[]{i, j});
            }
        }
        for (int i = 0; i < n; i++) {
            for (int j = 0; j < m; j++) writer.write(dist[i][j] + " ");
            writer.write("\n");
        }
    }
}
